/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
 */

/**
 * Created by yfyang on 5/27/14.
 */
define(['adminapp'], function ($app) {
    $app.navactive('repart_booking');
    var item_tpl = Handlebars.compile($('#item_template').html());


    var action_paths = {
        'list'  : g.ctx + 'admin/booking/market/list',
        'item'  : g.ctx + 'admin/booking/market/item',
        'remove': g.ctx + 'admin/booking/market/remove',
        'save'  : g.ctx + 'admin/booking/market/save'
    };

    var categorys = $('#grid_market');


    var initCategoryGrid = function () {
        $.get(action_paths.list + '/' + g.brand, function (rst) {
            if (rst.status == 'OK') {
                categorys.empty();
                var data = rst.data;
                $.each(data, function (id, val) {
                    val['status_show'] = (val['status'] == 0) ? '未启用': ((val['status'] == 1)? '正在使用': '已过期');
                    categorys.append(item_tpl(val));
                    var $remove_links = categorys.find('a.remove');
                    if ($remove_links.length > 0) {
                        require(['bootstrap-confirmation'], function () {
                            $remove_links.each(function () {
                                var $this = $(this);
                                initConfirmation($this);
                            })
                        })
                    }
                })
            }
        });
    };

    initCategoryGrid();
    function initConfirmation($this) {
        var data = $this.attr('data-data');
        var $info_pan = $this.closest('.col-md-4');
        $this.confirmation({
            'title'  : '您确定删除这个市别么',
            onConfirm: function () {
                $.post(action_paths.remove + '/' + data, {}, function (rst) {
                    if (rst.status == 'OK') {
                        $info_pan.remove();
                    }
                });
            }
        })
    }


    var $body = $('body');
    $body.removeClass("modal-open");
    categorys.on('click', 'a.edit', function (e) {
        var data = $(this).attr('data-data');
        require(['bootstrap-modalmanager', 'bootstrap-modal'], function () {

            $('body').modalmanager('loading');
            $modal.load(action_paths.item + '/' + data, {}, function () {
                $modal.modal();
                $modal.find('.form_datetime').each(function () {
                    var $this = $(this);
                    $this.datetimepicker({
                        language      : 'zh-CN',
                        autoclose     : true,
                        isRTL         : App.isRTL(),
                        format        : "yyyy-mm-dd hh:ii:ss",
                        pickerPosition: (App.isRTL() ? "bottom-right" : "bottom-left")
                    })
                });
                $("input[type=checkbox]").uniform();
            });
        })
    });


    var $modal = $('#category-modal');
    $('#add_market').click(function (e) {


        require(['bootstrap-modalmanager', 'bootstrap-modal'], function () {

            $('body').modalmanager('loading');
            $modal.load(action_paths.item, {brand: g.brand}, function () {
                $modal.modal();
                $modal.find('.form_datetime').each(function () {
                    var $this = $(this);
                    $this.datetimepicker({
                        language      : 'zh-CN',
                        autoclose     : true,
                        isInline      : false,
                        showMeridian  : true,
                        isRTL         : App.isRTL(),
                        format        : "yyyy-mm-dd hh:ii:ss",
                        pickerPosition: (App.isRTL() ? "bottom-right" : "bottom-left")
                    });
                });
                $("input[type=checkbox]").uniform();
            });
        })
    });

    $body.on('click', 'button#save', function () {
        var $form = $modal.find('.form-horizontal');
        $.post(action_paths.save, $form.serialize(), function (rst) {
            if (rst.status == 'OK') {
                $modal.modal('hide');
//                var itemTpl = item_tpl(rst.data);
//                categorys.append(itemTpl);
                initCategoryGrid();
            } else {
                $modal
                    .find('.modal-body')
                    .prepend('<div class="alert alert-info fade in">' + rst.message +
                        '<button type="button" class="close" data-dismiss="alert">&times;</button>' +
                        '</div>');
            }
        });
    });
    return {};
});